import { Router } from "express"
import { IcallbackFn, Ioracle } from "../../types"

export default (router: Router, oracle: Ioracle, callbackFn: IcallbackFn) => {
  router.post("/getCanteenList", (_req, res) => {
    //抓取资料
    let sql = " select a.work_num "
    sql +=
      " ,decode(a.title,'CadreFromTaiwan','臺幹','Ordinary','普通員工','Engineer','師級','Cadre','主管',a.title) title "
    sql += " ,decode(a.is_pregnant,'Y','孕妇',a.is_pregnant) pregnant_flag "
    sql += " ,a.dorm_no,decode(a.post_flag,'Y','已抛','未抛') post_flag "
    sql += " ,a.dept_code "
    sql += " ,b.name_in_chinese chinese_name "
    sql += " from xx_g00_canteen_data a,personnel_basic b "
    sql += " where a.work_num = b.no "
    sql += " and a.resign_flag is null "
    sql += " order by b.dept_code,b.no "

    callbackFn(res, oracle.connHrm, { sql })
  })

  router.post("/changeCanteenDorm",(req,res) => {
    const { dormNo } = req.body
    let sql
    if (dormNo === "C"){
      sql = " update xx_g00_canteen_data "
      sql += " set post_flag = 'N' "
      sql += " ,dorm_no = 'G' "
      sql += " where dorm_no = 'C' "
      sql += " and resign_flag is null "
    }else{
      sql = " update xx_g00_canteen_data "
      sql += " set post_flag = 'N' "
      sql += " ,dorm_no = 'C' "
      sql += " where dorm_no = 'G' "
      sql += " and work_num in ( "
      sql += " select badge_no from chaos.xx_g00_employee_card@ccprod "
      sql += " where line_id in (select line_id from chaos.xx_g00_bed@ccprod "
      sql += " where header_id = '1029')) "
      sql += " and resign_flag is null "
    }
    
    callbackFn(res, oracle.connHrm, { sql })
  })

  return router
}
